package com.sxhalo.fsms.exception;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.NoHandlerFoundException;

import com.sxhalo.fsms.util.ApiResult;

import io.jsonwebtoken.ExpiredJwtException;

@ControllerAdvice
public class GlobalExceptionHandler {

	private Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

	/**
	 **	其他异常
	 */
	
	@ResponseBody
	@ExceptionHandler(Exception.class)
	public String handleException(HttpServletRequest req, Exception e) {
		logger.error(e.getMessage(), e);
		return ApiResult.retFail(BaseEnumError.系统异常请联系管理员).toString(req);
    }
	
	/**
	 **	自定义异常
	 */
	@ResponseBody
	@ExceptionHandler(ExpiredJwtException.class)
	public String handleExpiredJwtException(HttpServletRequest req, ExpiredJwtException e) {
		logger.error(e.getMessage(), e);
		return ApiResult.retFail(BaseEnumError.登录过期).toString(req);
	}

	/**
	 **	自定义异常
	 */
	@ResponseBody
	@ExceptionHandler(CustomException.class)
	public String handleCommonException(HttpServletRequest req, CustomException e) {
		logger.error(e.getMessage(), e);
		return ApiResult.retFail(e.getCode(),e.getMessage()).toString(req);
	}
	
	/**
	 ** 404处理
	 */
	@ResponseBody
	@ExceptionHandler(NoHandlerFoundException.class)
	public String notFountHandler(HttpServletRequest req, NoHandlerFoundException e) {
		logger.error(e.getMessage(), e);
		return ApiResult.retFail(BaseEnumError.接口不存在).toString(req);
	}
}
